<template>
  <div class="production-stock-in-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>生产入库</span>
        </div>
      </template>

      <el-form :inline="true" :model="queryForm" class="search-form">
        <el-form-item label="工单号">
          <el-input v-model="queryForm.workOrderNo" placeholder="请输入工单号" />
        </el-form-item>
        <el-form-item label="产品编码">
          <el-input v-model="queryForm.productCode" placeholder="请输入产品编码" />
        </el-form-item>
        <el-form-item label="生产日期">
          <el-date-picker
            v-model="queryForm.dateRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          />
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="queryForm.status" placeholder="请选择状态">
            <el-option label="待入库" value="pending" />
            <el-option label="部分入库" value="partial" />
            <el-option label="已完成" value="completed" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table :data="tableData" border style="width: 100%">
        <el-table-column type="expand">
          <template #default="props">
            <el-table :data="props.row.items" border>
              <el-table-column prop="productCode" label="产品编码" />
              <el-table-column prop="productName" label="产品名称" />
              <el-table-column prop="specification" label="规格型号" />
              <el-table-column prop="planQuantity" label="计划数量" />
              <el-table-column prop="producedQuantity" label="已生产数量" />
              <el-table-column prop="stockInQuantity" label="已入库数量" />
              <el-table-column prop="remainQuantity" label="待入库数量" />
            </el-table>
          </template>
        </el-table-column>
        <el-table-column prop="workOrderNo" label="工单号" width="180" />
        <el-table-column prop="productionLine" label="生产线" />
        <el-table-column prop="productionDate" label="生产日期" width="180" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="scope">
            <el-tag :type="getStatusType(scope.row.status)">
              {{ getStatusText(scope.row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150" fixed="right">
          <template #default="scope">
            <el-button 
              link 
              type="primary" 
              @click="handleStockIn(scope.row)"
              v-if="['pending', 'partial'].includes(scope.row.status)"
            >入库</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 入库操作对话框 -->
    <el-dialog
      v-model="stockInDialogVisible"
      title="生产入库"
      width="80%"
    >
      <el-form :model="stockInForm" label-width="100px">
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="工单号">
              <el-input v-model="stockInForm.workOrderNo" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="生产线">
              <el-input v-model="stockInForm.productionLine" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="入库仓库" required>
              <el-select v-model="stockInForm.warehouse" placeholder="请选择仓库">
                <el-option
                  v-for="item in warehouseOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-table :data="stockInForm.items" border>
          <el-table-column prop="productCode" label="产品编码" />
          <el-table-column prop="productName" label="产品名称" />
          <el-table-column prop="specification" label="规格型号" />
          <el-table-column prop="remainQuantity" label="待入库数量" />
          <el-table-column label="本次入库数量" width="150">
            <template #default="scope">
              <el-input-number
                v-model="scope.row.currentQuantity"
                :min="0"
                :max="scope.row.remainQuantity"
              />
            </template>
          </el-table-column>
          <el-table-column label="库位" width="150">
            <template #default="scope">
              <el-select v-model="scope.row.location" placeholder="请选择库位">
                <el-option
                  v-for="item in locationOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="生产批次" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.batchNo" placeholder="请输入生产批次" />
            </template>
          </el-table-column>
          <el-table-column label="质检状态" width="120">
            <template #default="scope">
              <el-tag :type="getQcStatusType(scope.row.qcStatus)">
                {{ getQcStatusText(scope.row.qcStatus) }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column label="备注" width="200">
            <template #default="scope">
              <el-input v-model="scope.row.remark" placeholder="请输入备注" />
            </template>
          </el-table-column>
        </el-table>

        <el-form-item label="备注" class="mt-20">
          <el-input v-model="stockInForm.remark" type="textarea" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="stockInDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleStockInSubmit">确认入库</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'

// ... 省略状态定义 ...

const handleStockInSubmit = () => {
  if (!stockInForm.warehouse) {
    ElMessage.warning('请选择入库仓库')
    return
  }

  // 验证数据
  const valid = stockInForm.items.every(item => {
    if (item.currentQuantity > 0) {
      if (!item.location) {
        ElMessage.warning(`${item.productName} 请选择库位`)
        return false
      }
      if (!item.batchNo) {
        ElMessage.warning(`${item.productName} 请输入生产批次`)
        return false
      }
    }
    return true
  })

  if (!valid) return

  // 验证是否有入库数量
  const hasQuantity = stockInForm.items.some(item => item.currentQuantity > 0)
  if (!hasQuantity) {
    ElMessage.warning('请输入入库数量')
    return
  }

  // 实现入库逻辑
  ElMessage.success('入库成功')
  stockInDialogVisible.value = false
}
</script>

<style scoped>
.production-stock-in-container {
  padding: 20px;
}

.search-form {
  margin-bottom: 20px;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.mt-20 {
  margin-top: 20px;
}
</style> 